﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CourseExamples
{
	/// <summary>
	/// This class is for the second class on topic: Data Structure, including
	/// Array, List, Queue, Stack, Tree and Graph.
	/// </summary>
	public class C02_DataStructure
	{
		public void Run()
		{
			ArrayDemo();
			ListDemo();
			ArrayVSList();
		}


		public void ArrayDemo()
		{
			Console.WriteLine("Array Demo");
			int[] array = { 1, 2, 3 };
			for (int i = 0; i < array.Length; i++)
				Console.WriteLine(array[i]);
		}

		public void ListDemo()
		{
			Console.WriteLine("List Demo");
			List<int> list = new List<int>() { 1, 2, 3 };
			for (int i = 0; i < list.Count; i++)
				Console.WriteLine(list[i]);
		}

		public void ArrayVSList()
		{
			int size = 100_000_000;
			double[] array = new double[size];
			List<double> list = new List<double>();
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = Math.Sqrt(i);
				list.Add(Math.Sqrt(i));
			}

			double sum1 = 0;
			DateTime dt1 = DateTime.Now;
			for (int i = 0; i < array.Length; i++)
			{
				sum1 += array[i];
			}
			DateTime dt2 = DateTime.Now;

			double sum2 = 0;
			DateTime dt3 = DateTime.Now;
			for (int i = 0; i < list.Count; i++)
			{
				sum2 += list[i];
			}
			DateTime dt4 = DateTime.Now;

            Console.WriteLine($"sum1={sum1:0.00}\nsum2={sum2:0.00}");
			Console.WriteLine($"Time for array: {(dt2 - dt1).TotalMilliseconds:0}ms.");
			Console.WriteLine($"Time for list:  {(dt4 - dt3).TotalMilliseconds:0}ms.");
		}


	}
}
